package cn.tedu._03vehice.controller;

import cn.tedu._03vehice.base.response.JsonResult;
import cn.tedu._03vehice.base.response.StatusCode;
import cn.tedu._03vehice.mapper.VehicleMapper;
import cn.tedu._03vehice.pojo.dto.VehicleAddParam;
import cn.tedu._03vehice.pojo.dto.VehicleListQuery;
import cn.tedu._03vehice.pojo.entity.Vehicle;
import cn.tedu._03vehice.pojo.vo.UserVO;
import cn.tedu._03vehice.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@Api(tags = "02.车辆管理模块")
@RestController
@RequestMapping("/v1/vehicle/")public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation("添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        log.debug("vehicleAddParam = " + vehicleAddParam);
        /*
            校验用户的登录状态: session.getAttribute("user");
            1.未登录: 直接返回登录失败的JsonResult;
            2.登录: 继续执行添加车辆功能[INSERT接口 insertVehicle(实体类...)];
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO = " + userVO);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());

        vehicleMapper.insertVehicle(vehicle);

        return JsonResult.ok();
    }

    /**
     * 2.车辆列表功能
     */
    @GetMapping("list")
    @ApiOperation("车辆列表功能")
    @ApiOperationSupport(order = 20)
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("vehicleListQuery = " + vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //3个查询条件
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVO.getId();

        List<VehicleListVO> list = vehicleMapper.listVehicle(brand, license, userId);

        return JsonResult.ok(list);
    }

    /**
     * 3.删除车辆功能
     */
    @PostMapping("del/{id}")
    @ApiOperation("删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult delVehicle(@Range(min = 1, max = 100, message = "车辆编号必须在1-100之间") @PathVariable Long id, @ApiIgnore HttpSession session){
        //手动抛异常
        if(id<0){
            throw new IllegalArgumentException("id值非法!");
        }
        log.debug("id = " + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Long userId = userVO.getId();
        int i = vehicleMapper.delVehicle(id, userId);
        if (i > 0){
            return JsonResult.ok();
        } else {
            // 非法请求,该用户正在尝试删除其他人的车辆
            return new JsonResult(StatusCode.NOT_ALLOWED);
        }
    }
}





